package com.tsx.kafka.cunsumer;

import java.util.Arrays;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

public abstract class AbstractReceiver extends Thread {

	
	
	private void receiverDataKa() {
        Properties props = new Properties();
//        props.put("bootstrap.servers", "172.17.6.10:9092,172.17.6.11:9092,172.17.6.12:9092");// kafka 集群
//        props.put("zookeeper.connect", "172.17.6.10:2181,172.17.6.11:2181,172.17.6.12:2181");// zookeeper集群负载
      props.put("bootstrap.servers", "172.16.6.206:9092");// kafka 单机版
      props.put("zookeeper.connect", "172.16.6.206:2181");//zookeeper单机版
       props.put("group.id", "echo_study"); // 归属群组Id
        props.put("enable.auto.commit", "true");
        props.put("auto.commit.interval.ms", 1000);
        props.put("session.timeout.ms", "30000");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList(topic()));
        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(50);
            for (ConsumerRecord<String, String> record : records) {
            	toDealWith(record);
            }
        }
	}
	
	public abstract String topic();
	public abstract void toDealWith(ConsumerRecord<String, String> record ) ;
	
	@Override
	public void run() {
		receiverDataKa();
	}
}
